﻿using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using wlog.Model;

namespace wlog.Shell.Storage.Blob
{
    public class BlobCreateCommand : BlobStorageCommand
    {
       
        public BlobCreateCommand()
        {
            TransferType = TransferTypeEnum.Upload;
        }
        public override void Setup(string[] args)
        {
            base.Setup(args);
            containerName = objectName;
            blobName = additionalCommands[K_OPTIONS.objectname];
            base.CreateTask(() => { RunCommand(); });
        }
        public OutputChannel RunCommand()
        {
            try
            {
                CloudBlobClient blobAPI = GetBlobAccount();
                Results.WriteLine("Connected to: {0}", blobAPI.BaseUri);
                var root = blobAPI.GetContainerReference(containerName);

                var containerExists = root.CreateIfNotExists();
                var permissions = root.GetPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                root.SetPermissions(permissions);
                if (containerExists)
                {
                    Results.WriteLine("Blob Containers exists: {0}", containerName);
                }
                else
                {
                    Results.WriteLine("Blob Containers created: {0}", containerName);
                }
                if (!string.IsNullOrEmpty(blobName))
                {
                    Results.WriteLine("Lets Create a blob {0} on the Containers created: {1}", blobName, containerName);
                    var blob = root.GetBlockBlobReference(additionalCommands[K_OPTIONS.objectname]);
                    blob.Properties.ContentType = "text/plain";

                    // I am using a syncronous uploading here...
                    bool isThere = blob.Exists();
                    if (isThere)
                    {
                        Results.WriteLine("Blob exists: {0}", additionalCommands[K_OPTIONS.objectname]);
                    }
                    else
                    {
                        Results.WriteLine("Blob does not exists: {0}", additionalCommands[K_OPTIONS.objectname]);
                    }
                    Results.WriteLine("Uploading ... {0}", additionalCommands[K_OPTIONS.objecturi]);
                    if (async)
                    {
                        UploadAsync(filePath, blob);
                    }
                    else
                    {
                        UploadSync(filePath, blob);
                    }

                    Results.WriteLine("Written to... {0}", blob.Uri.ToString());
                }

            }
            catch (Exception azureEx)
            {
                Results.WriteLine("Error ccreating Blob {0} ", azureEx.Message);
            }
            return Results;
        }
    }
}
